import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:my_desk_flutter/controller/auth_controller.dart';

class LoginPage extends ConsumerWidget {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final auth = ref.watch(authControllerProvider);
    ref.listen(authControllerProvider, (previous, next) {
      if (next.value?.isAuth ?? false) {
        Navigator.of(context).pop();
      }
    });
    return Scaffold(
      appBar: AppBar(
        title: const Text("登录页面"),
      ),
      body: Padding(
        padding: EdgeInsets.all(15.w),
        child: Column(
          children: [
            Row(
              children: [
                switch (auth.value) {
                  SignedIn(:final id) => Text("登录帐号:$id"),
                  _ => const Text("未登录"),
                },
                const SizedBox(width: 10),
                if (auth.isLoading) const CupertinoActivityIndicator(),
              ],
            ),
            SizedBox(height: 20.h),
            ElevatedButton(
              child: const Text("登录"),
              onPressed: () {
                ref
                    .read(authControllerProvider.notifier)
                    .login('lamp0', '124536');
              },
            ),
            SizedBox(height: 20.h),
            ElevatedButton(
              child: const Text("退出登录"),
              onPressed: () {
                ref.read(authControllerProvider.notifier).logout();
              },
            ),
            ElevatedButton(
              child: const Text("刷新当前provider"),
              onPressed: () {
                ref.read(authControllerProvider.notifier).refreshCurrent();
              },
            ),
          ],
        ),
      ),
    );
  }
}
